 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class GenerateTestSql {

    private static String userparty_sql="INSERT INTO userdb.user_party (party_id, party_name, party_key, state, create_time, update_time, phone_state) VALUES ($PARTY_ID, 'TEST COMPANY $PARTY_ID', '$PARTY_ID', 1, NOW(), NULL, 1);";
    private static String userpartycompany_sql="INSERT INTO userdb.user_party_company (company_id, party_id, company_name, company_address, account, user_name, state, create_time, update_time, free_type, auth_number, record, agent_id, tel, company_code) VALUES ($PARTY_ID, $PARTY_ID, 'TEST COMPANY $PARTY_ID', NULL, NULL, '$PARTY_ID', 1, NOW(), NULL, 1, -1, 1, 1, '', NULL);";
    private static String userorg_sql="INSERT INTO userdb.user_org (org_id, org_code, party_id, org_name, parent_id, sortno, leaf, leaf_num, state, create_time, update_time, version) VALUES ($PARTY_ID, '$ORG_CODE', $PARTY_ID, 'TEST COMPANY $PARTY_ID ORG', 0, 0, NULL, NULL, 1, NOW(), NULL, NULL);";
    private static String userbasic_sql="INSERT INTO userdb.user_basic (uid, user_number, user_name, user_type, sex, v_face, terminal_type, terminal, phone, password, short_number, IMEI, enabled, state, create_time, update_time, version, party_id, org_id, org_code, visible, district_id, district_type, expired_date, iccid, app_version, display_state, loc_flag, loc_interval, mobile) VALUES ";
    private static String userbasicdtl_sql="($U_ID, NULL, '$U_ID', 1, 1, 0, NULL, '$U_ID', '$U_ID', '9db06bcff9248837f86d1a6bcf41c9e7', NULL, NULL, 0, 1, NOW(), NULL, 1, $PARTY_ID, $PARTY_ID, '$ORG_CODE', 0, NULL, NULL, '2018-12-16 00:00:00', NULL, NULL, 99, 1, 60, NULL)";
    private static String roombasic_sql="INSERT INTO roomdb.room_basic (rid, name, password, style, capacity, expire_type, login_type, enter_ctrl, offline_ctrl, ptt_ctrl, text_ctrl, audio_ctrl, video_ctrl, media_ctrl, play_ctrl, layout, video_id, audio_id, owner_id, intro, note, create_time, update_time, version, party_id, org_id, org_code, win1, win2, win3, win4, win5, win6, win7, win8, win9, win10, win11, win12, state, hour, record) VALUES ($R_ID, '$R_NAME', NULL, 3, 1000, 1, 1, 1, 1, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, $OWER_ID, NULL, NULL, NOW(), NULL, NULL, $PARTY_ID, $PARTY_ID, '$ORG_CODE', 100, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 1, 0, 0);";
    //private static String roommember_sql="INSERT INTO roomdb.room_member (rid, uid, user_name, admin_grade, priority, timelen, videolen, version) VALUES ($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, 0);";
    private static String roommember_sql="INSERT INTO roomdb.room_member (rid, uid, user_name, admin_grade, priority, timelen, videolen, version,state) VALUES ";
    private static String roommemberdtl_sql="($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, $U_ID,1)";
    public static void main(String[] args) {
//        int partyId = "1000";
//        String orgCode = "00"+partyId;
//        generateCompany(partyId,orgCode);
//        generateUser(partyId,orgCode);
//        System.out.println(generateRoom1000(partyId,orgCode));
//        generateRoom500(partyId,orgCode,1);
//        generateRoom500(partyId,orgCode,2);
//        for (int i = 1; i < 11; i++) {
//            generateRoom100(partyId,orgCode,i);
//        }
//        for (int i = 1; i < 21; i++) {
//            generateRoom50(partyId,orgCode,i);
//        }
//
//        for (int i = 1; i < 101; i++) {
//            generateRoom10(partyId,orgCode,i);
//
//        }
//        System.out.println(generateCompanyTotal("1000"));
//        WriteStringToFile("/Users/wangluyin/1001.sql",generateCompanyTotal("1001").toString());
        //System.out.println(generateRoom500(1000,"001000",2));
        for (int i = 0; i < 1; i++) {
            int partyId = 1000+i;
            //int fileId=  partyId/100;
            WriteStringToFile("/data/dbuser/batch_test.sql",generateCompanyTotal(partyId).toString());
            System.out.println(i);
        }

    }

    public static void WriteStringToFile(String filePath,String str) {
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file,true));
            ps.append(str);// 往文件里写入字符串

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static StringBuilder generateCompanyTotal(int partyId) {
        StringBuilder sb = new StringBuilder();
        String orgCode = "00"+partyId;
//        sb.append(generateCompany(partyId,orgCode));
//        sb.append("\n");
//        sb.append(generateUser(partyId,orgCode));
//        sb.append("\n");
        sb.append(generateRoom1000(partyId,orgCode));
        sb.append("\n");
        sb.append(generateRoom500(partyId,orgCode,1));
        sb.append("\n");
        sb.append(generateRoom500(partyId,orgCode,2));
        sb.append("\n");
        for (int i = 1; i < 11; i++) {
            sb.append(generateRoom100(partyId,orgCode,i));
            sb.append("\n");
        }
        for (int i = 1; i < 21; i++) {
            sb.append(generateRoom50(partyId,orgCode,i));
            sb.append("\n");
        }

        for (int i = 1; i < 101; i++) {
            sb.append(generateRoom10(partyId,orgCode,i));
            sb.append("\n");
        }
        return sb;
    }

    public static StringBuilder generateCompany(int partyId,String orgCode) {
        StringBuilder sb = new StringBuilder();
        sb.append(userparty_sql.replaceAll("\\$PARTY_ID",partyId+""));
        sb.append("\n");
        sb.append(userpartycompany_sql.replaceAll("\\$PARTY_ID",partyId+""));
        sb.append("\n");
        sb.append(userorg_sql.replaceAll("\\$PARTY_ID",partyId+"").replaceAll("\\$ORG_CODE",orgCode));
        sb.append("\n");
        return sb;

    }
    public static StringBuilder generateUser(int partyId,String orgCode) {
        StringBuilder sb = new StringBuilder();
        sb.append(userbasic_sql);
        int max = 1001;
        for(int i=1;i<1001;i++) {
            String uid = String.valueOf(partyId*10000+i);
            sb.append(userbasicdtl_sql.replaceAll("\\$PARTY_ID", partyId+"").replaceAll("\\$ORG_CODE", orgCode).replaceAll("\\$U_ID", uid));
            if(i< max-1) {
                sb.append(",\n");
            }else{
                sb.append(";\n");
            }
        }
        return sb;
    }
    public static StringBuilder generateRoom1000(int partyId,String orgCode) {
        StringBuilder sb = new StringBuilder();
        String rid = partyId+"1000";
        String rname = "Group[1000]";
        String ower_id = partyId+"0001";
//        sb.append(roombasic_sql.replaceAll("\\$PARTY_ID", partyId+"").replaceAll("\\$ORG_CODE", orgCode).replaceAll("\\$R_ID", rid).replaceAll("\\$R_NAME", rname).replaceAll("\\$OWER_ID", ower_id));
//        sb.append("\n");
        int max = 1001;
        sb.append(roommember_sql);
        for(int i=1;i<max;i++) {
            String uid = String.valueOf(partyId*10000+i);
            sb.append(roommemberdtl_sql.replaceAll("\\$R_ID", rid).replaceAll("\\$U_ID", uid));
           // ($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, 0);
            if(i< max-1) {
                sb.append(",\n");
            }else{
                sb.append(";\n");
            }
        }
        return sb;
    }

    public static StringBuilder generateRoom500(int partyId, String orgCode,int index) {
        StringBuilder sb = new StringBuilder();
        String rid = partyId + "500"+index;
        String rname = "Group[500]_"+index;
        String ower_id = partyId+"0001";
//        sb.append(roombasic_sql.replaceAll("\\$PARTY_ID", partyId+"").replaceAll("\\$ORG_CODE", orgCode).replaceAll("\\$R_ID", rid).replaceAll("\\$R_NAME", rname).replaceAll("\\$OWER_ID", ower_id));
//
//        sb.append("\n");
        int start = (index-1)*500+1;
        int max = 500*index+1;
        sb.append(roommember_sql);
        for(int i=start;i<max;i++) {
            String uid = String.valueOf(partyId*10000+i);
            sb.append(roommemberdtl_sql.replaceAll("\\$R_ID", rid).replaceAll("\\$U_ID", uid));
            // ($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, 0);
            if(i< max-1) {
                sb.append(",\n");
            }else{
                sb.append(";\n");
            }
        }
        return sb;
    }

    public static StringBuilder generateRoom100(int partyId, String orgCode,int index) {
        StringBuilder sb = new StringBuilder();
        String rid = partyId + "100"+index;
        String rname = "Group[100]_"+index;
        String ower_id = partyId+"0001";
//        sb.append(roombasic_sql.replaceAll("\\$PARTY_ID", partyId+"").replaceAll("\\$ORG_CODE", orgCode).replaceAll("\\$R_ID", rid).replaceAll("\\$R_NAME", rname).replaceAll("\\$OWER_ID", ower_id));
//
//        sb.append("\n");
        int start = (index-1)*100+1;
        int max = 100*index+1;
        sb.append(roommember_sql);
        for(int i=start;i<max;i++) {
            String uid = String.valueOf(partyId*10000+i);
            sb.append(roommemberdtl_sql.replaceAll("\\$R_ID", rid).replaceAll("\\$U_ID", uid));
            // ($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, 0);
            if(i< max-1) {
                sb.append(",\n");
            }else{
                sb.append(";\n");
            }
        }
        return sb;
    }

    public static StringBuilder generateRoom50(int partyId, String orgCode,int index) {
        StringBuilder sb = new StringBuilder();
        String rid = partyId + "50"+index;
        String rname = "Group[50]_"+index;
        String ower_id = partyId+"0001";
//        sb.append(roombasic_sql.replaceAll("\\$PARTY_ID", partyId+"").replaceAll("\\$ORG_CODE", orgCode).replaceAll("\\$R_ID", rid).replaceAll("\\$R_NAME", rname).replaceAll("\\$OWER_ID", ower_id));
//
//        sb.append("\n");
        int start = (index-1)*50+1;
        int max = 50*index+1;
        sb.append(roommember_sql);
        for(int i=start;i<max;i++) {
            String uid = String.valueOf(partyId*10000+i);
            sb.append(roommemberdtl_sql.replaceAll("\\$R_ID", rid).replaceAll("\\$U_ID", uid));
            // ($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, 0);
            if(i< max-1) {
                sb.append(",\n");
            }else{
                sb.append(";\n");
            }
        }
        return sb;
    }

    public static StringBuilder generateRoom10(int partyId, String orgCode,int index) {
        StringBuilder sb = new StringBuilder();
        String rid = partyId + "10"+index;
        String rname = "Group[10]_"+index;
        String ower_id = partyId+"0001";
//        sb.append(roombasic_sql.replaceAll("\\$PARTY_ID", partyId+"").replaceAll("\\$ORG_CODE", orgCode).replaceAll("\\$R_ID", rid).replaceAll("\\$R_NAME", rname).replaceAll("\\$OWER_ID", ower_id));
//
//        sb.append("\n");
        int start = (index-1)*10+1;
        int max = 10*index+1;
        sb.append(roommember_sql);
        for(int i=start;i<max;i++) {
            String uid = String.valueOf(partyId*10000+i);
            sb.append(roommemberdtl_sql.replaceAll("\\$R_ID", rid).replaceAll("\\$U_ID", uid));
            // ($R_ID, $U_ID, '$U_ID', 5, 2, 120, 0, 0);
            if(i< max-1) {
                sb.append(",\n");
            }else{
                sb.append(";\n");
            }
        }
        return sb;
    }

}
